package com.cc.controller;


import com.cc.model.EbNews;
import com.cc.model.EbProduct;
import com.cc.model.EbProductCategory;
import com.cc.service.NewsService;
import com.cc.service.ProductCategoryService;
import com.cc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/index")
public class IndexControler {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategory;

    @Autowired
    private NewsService newsService;
    @RequestMapping("/manage")
    public String manage() {
        return "manage/index";
    }


    @RequestMapping("/front")
    public ModelAndView index(Model model,@RequestParam(value = "page",defaultValue = "1") int page,
                              @RequestParam(value = "pageSize",defaultValue = "8")int pageSize) {
        List<EbProduct> productList = productService.list(page,pageSize);
        //2.将数据（List）和视图（jsp）返回给中央控制器
        ModelAndView mv = new ModelAndView();
        //获取最大页数
        int maxPage = productService.getMaxPage(pageSize);
//=========================================
        List<EbProductCategory> findBigProductCategory =  productCategory.findBigProductCategory();

        List<EbProductCategory> findSmallProductCategory = productCategory.findSmallProductCategory();


        List<EbNews> ebNews =  newsService.findNews();

        mv.addObject("ebNews",ebNews);


 //================================

        //绑定数据
        mv.addObject("productList",productList);

        mv.addObject("findBigProductCategory",findBigProductCategory);

        mv.addObject("findSmallProductCategory",findSmallProductCategory);
        //绑定当前页


        mv.addObject("curPage",page);
        //b绑定最大页
        mv.addObject("maxPage",maxPage);
        //绑定视图
        mv.setViewName("index");
        return mv;

    }


}
